<?php

/**
 * Created by PhpStorm.
 * User: 董兴涛
 * Date: 2020/7/18
 * Time: 14:05
 */

namespace app\engine\controller;

use cmf\controller\HomeBaseController;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;

class StaffController extends HomeBaseController
{
    /**
     * 加载内部员工首页
     * @return mixed|string
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function getStaff()
    {

        $data = Db::name('staff')->select()->toArray();
        $this->assign('data', $data);
        //        $this->assign('id', $data['id']);
        return $this->fetch();
    }

    /**
     * 添加内部员工页
     * @return mixed|string
     */
    public function add()
    {
        // $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        // if($role == 2 || $role == 5 || $role == 3){
        //     $this->error('无权限操作');
        // }
        return $this->fetch();
    }

    /**
     * 执行添加
     */
    public function doadd()
    {
        $data = $this->request->param();
        $files = request()->file('image');
        unset($data['image']);
        $kps = [];

        if (empty($data['name']) || empty($data['mobile']) || empty($data['card']) || empty($data['address']) || empty($data['position']) || empty($data['insTime'])) {
            $this->error('请填写参数');
        }
        if ($files) {
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(CMF_ROOT . 'public' . '/' . 'uploads');
                if ($info) {
                    $kps[$k] = '/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data['imgs'] = implode(',', $kps);
        }
        if ($data['status'] == 2) {
            $data['outTime'] = date('Y-m-d', time());
        }
        $checkName = Db::name('staff')->where(['name' => $data['name']])->find();
        if ($checkName) {
            $this->error('该姓名已存在');
        }
        $checkMobile = Db::name('staff')->where(['mobile' => $data['mobile']])->find();
        if ($checkMobile) {
            $this->error('该手机号已存在');
        }
        $res = Db::name('staff')->insert($data);
        if (!$res) {
            $this->error('添加失败');
        }
        $this->success('添加成功');
    }

    /**
     * 加载内部员工详情
     * @return mixed|string
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function see()
    {
        $data = $this->request->param();
        $res = Db::name('staff')->where('id', $data['id'])->find();
        $res['imgs'] = explode(',', $res['imgs']);
        foreach ($res['imgs'] as $k => &$v) {
            $v = 'http://' . $_SERVER['SERVER_NAME'] . $v;
        }
        $res['insTime'] = date('Y-m-d', strtotime($res['insTime']));
        $this->assign('id', $data['id']);
        $this->assign('res', $res);
        return $this->fetch();
    }

    /**
     * 执行编辑
     * @throws Exception
     * @throws PDOException
     */
    public function edit()
    {
        // $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        // if($role == 2 || $role == 5 || $role == 3){
        //     $this->error('无权限操作');
        // }
        $data = $this->request->param();
        $id = $data['id'];
        unset($data['id']);
        $files = request()->file('image');
        if ($files) {
            $check = Db::name('staff')->where('id', $id)->value('imgs');
            $cp = explode(',', $check);
            foreach ($cp as $v) {
                $path = CMF_ROOT . 'public' . '/' . $v;
                if (file_exists($path)) {
                    unlink($path);
                }
            }
            $kps = [];
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(CMF_ROOT . 'public' . '/' . 'uploads');
                if ($info) {
                    $kps[$k] = '/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data['imgs'] = implode(',', $kps);
        } else {
            unset($data['image']);
        }

        if (empty($data['name']) || empty($data['mobile']) || empty($data['card']) || empty($data['address']) || empty($data['position']) || empty($data['insTime'])) {
            $this->error('请填写参数');
        }

        $res = Db::name('staff')->where('id', $id)->update($data);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }


    /**
     * 删除内部员工
     * @throws Exception
     * @throws PDOException
     */
    public function del()
    {
        // $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        // if($role == 2 || $role == 5 || $role == 3){
        //     $this->error('无权限操作');
        // }
        $data = $this->request->param();
        $res = Db::name('staff')->where('id', $data['id'])->delete();
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }
}
